#include"CProcTextureChecker.h"
#include <math.h>

//for the memcpy
#include <cstring>

CProcTextureChecker::CProcTextureChecker()
{
	m_colorChecker[0] = m_colorChecker[1] = CColor(0, 0, 0, 1);
	m_length = 0;
	m_map = CVector3f(0, 1, 1);
}

CProcTextureChecker::CProcTextureChecker(CColor* colorChecker, float length, const CVector3f& map):m_length(length),
		m_map(map)
{
	memcpy(m_colorChecker, colorChecker, 2 * sizeof(CColor)); 
}

CProcTextureChecker::CProcTextureChecker(const CColor& color1, const CColor& color2, float length, 
										 const CVector3f& map)
										 :m_length(length), m_map(map)
{
	m_colorChecker[0] = color1;
	m_colorChecker[1] = color2;
}

CColor CProcTextureChecker::getColor(const CVector3f& p)const
{
	CVector3f newP = m_transform * p;

	float u, v;
	if(m_map.x != 0 && m_map.y != 0)
	{
		u = newP.x;
		v = newP.y;
	}
	else if (m_map.x != 0 && m_map.z != 0)
	{
		u = newP.x;
		v = newP.z;
	}
	else
	{
		u = newP.y;
		v = newP.z;
	}

	//check parity of u
	int ratio = (int)((int)u / m_length);
	int modulo = ratio % 2;
	bool uEven = true;
	if(modulo != 0)
		uEven = false;
	if(u < 0)
		uEven = !uEven;

	//check parity of v
	ratio = (int)((int)v / m_length);
	modulo = ratio % 2;
	bool vEven = true;
	if(modulo != 0)
		vEven = false;
	if(v < 0)
		vEven = !vEven;


	if((vEven && uEven) || (!vEven && !uEven))
		return m_colorChecker[0];
	else
		return m_colorChecker[1];
}